
package com.hlkj.pay.controller.pay;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.Collections;
import java.util.List;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.pay.IPayWayAppService;
import com.hlkj.pay.app.transfer.PayWayDoToResp;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.pay.PayWayQueryDto;
import com.hlkj.pay.infrastructure.model.pay.PayWayDO;
import com.hlkj.pay.service.pay.IPayWayService;
import com.hlkj.pay.vo.common.req.DeleteCodeReq;
import com.hlkj.pay.vo.merchant.req.UpdateStatusReq;
import com.hlkj.pay.vo.pay.req.PayWayPageQueryReq;
import com.hlkj.pay.vo.pay.req.PayWayReq;
import com.hlkj.pay.vo.pay.resp.PayWayResp;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.BeanUtil;

/**
 * @author HlpayTeam
 * @date 2024/09/23 11:33
 */
@Tag(name = "支付方式配置")
@RestController
@RequestMapping("/v1/pay/way")
@Slf4j
@RequiredArgsConstructor
public class PayWayController extends BaseController {

    private final IPayWayService payWayService;

    private final IPayWayAppService payWayAppService;
    
    @Operation(summary = "分页查询")
    @GetMapping("/page")
    CommonResult<PageResult<PayWayResp>> queryPayWayPage(@Validated PayWayPageQueryReq pageParam) {
        PayWayQueryDto payWayQueryDto = BeanUtil.copy(pageParam, PayWayQueryDto.class);
        payWayQueryDto.setLikeCode(pageParam.getCode());
        payWayQueryDto.setLikeName(pageParam.getName());
        payWayQueryDto.setCode(null);
        payWayQueryDto.setName(null);
        PageResult<PayWayDO> page=payWayService.queryPage(payWayQueryDto);
        List<PayWayDO> list = page.getList();
        if(CollectionUtils.isEmpty(list)){
            return buildPageResult(page, Collections.emptyList());
        }
        return buildPageResult(page, PayWayDoToResp.INSTANCE.payWayRespList(list));
    }

    @Operation(summary = "不分页查询")
    @GetMapping("")
    CommonResult<List<PayWayResp>> queryPayWayList(@Validated PayWayPageQueryReq pageParam) {
        PayWayQueryDto payWayQueryDto = BeanUtil.copy(pageParam, PayWayQueryDto.class);
        payWayQueryDto.setLikeCode(pageParam.getCode());
        payWayQueryDto.setLikeName(pageParam.getName());
        payWayQueryDto.setCode(null);
        payWayQueryDto.setName(null);
        List<PayWayDO> payWayList=payWayService.queryList(payWayQueryDto);
        if(CollectionUtils.isEmpty(payWayList)){
            return CommonResult.success(Collections.emptyList());
        }
        return CommonResult.success(PayWayDoToResp.INSTANCE.payWayRespList(payWayList));
    }
    @Operation(summary = "支付方式详情")
    @GetMapping("/detail")
    CommonResult<PayWayResp> detail(@RequestParam("code") String code) {
        PayWayDO payWayDO=payWayService.queryByCode(code);
        if(payWayDO==null){
            return CommonResult.success(null);
        }
        return CommonResult.success(PayWayDoToResp.INSTANCE.payWayResp(payWayDO));
    }
    @Operation(summary = "新增支付方式")
    @PostMapping("")
    CommonResult<Void> savePayWay(@Validated @RequestBody PayWayReq payWayReq) {
        return payWayAppService.savePayWay(payWayReq);
    }
    @Operation(summary = "更新支付方式")
    @PutMapping("")
    CommonResult<Void> updatePayWay(@Validated @RequestBody PayWayReq payWayReq) {
        return payWayAppService.updatePayWay(payWayReq);
    }
    @Operation(summary = "删除支付方式")
    @DeleteMapping("")
    CommonResult<Void> delPayWay(@Validated @RequestBody DeleteCodeReq deleteCodeReq) {
        return payWayAppService.delPayWay(deleteCodeReq.getCode());
    }
    @Operation(summary = "更新支付方式状态")
    @PostMapping("/status")
    CommonResult<Void> updatePayWayStatus(@Validated @RequestBody UpdateStatusReq updateStatusReq) {
        return payWayAppService.updatePayWayStatus(updateStatusReq);
    }
}
